#####################################################################################################
# Muslim Immigrants

m.m.2.1 <- lm(liberal ~ rel_banperm + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.1[imp.data.1$rel_muslim==1 & imp.data.1$rel_immigrant==1,])
m.y.2.1 <- lm(rel_feel ~ rel_banperm*liberal + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.1[imp.data.1$rel_muslim==1 & imp.data.1$rel_immigrant==1,], x=T)

m.m.2.2 <- lm(liberal ~ rel_banperm + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.2[imp.data.2$rel_muslim==1 & imp.data.2$rel_immigrant==1,])
m.y.2.2 <- lm(rel_feel ~ rel_banperm*liberal + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.2[imp.data.2$rel_muslim==1 & imp.data.2$rel_immigrant==1,], x=T)

m.m.2.3 <- lm(liberal ~ rel_banperm + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.3[imp.data.3$rel_muslim==1 & imp.data.3$rel_immigrant==1,])
m.y.2.3 <- lm(rel_feel ~ rel_banperm*liberal + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.3[imp.data.3$rel_muslim==1 & imp.data.3$rel_immigrant==1,], x=T)

m.m.2.4 <- lm(liberal ~ rel_banperm + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.4[imp.data.4$rel_muslim==1 & imp.data.4$rel_immigrant==1,])
m.y.2.4 <- lm(rel_feel ~ rel_banperm*liberal + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.4[imp.data.4$rel_muslim==1 & imp.data.4$rel_immigrant==1,], x=T)

m.m.2.5 <- lm(liberal ~ rel_banperm + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.5[imp.data.5$rel_muslim==1 & imp.data.5$rel_immigrant==1,])
m.y.2.5 <- lm(rel_feel ~ rel_banperm*liberal + rel_dev + rel_rad + fem + I(age/10) + edu_high + religiosity, data=imp.data.5[imp.data.5$rel_muslim==1 & imp.data.5$rel_immigrant==1,], x=T)

ACME.1 <- ACME.fun(m.m.2.1, m.y.2.1)
ACME.2 <- ACME.fun(m.m.2.2, m.y.2.2)
ACME.3 <- ACME.fun(m.m.2.3, m.y.2.3)
ACME.4 <- ACME.fun(m.m.2.4, m.y.2.4)
ACME.5 <- ACME.fun(m.m.2.5, m.y.2.5)

c.1 <- apply(ACME.1, 2, mean)
c.2 <- apply(ACME.2, 2, mean)
c.3 <- apply(ACME.3, 2, mean)
c.4 <- apply(ACME.4, 2, mean)
c.5 <- apply(ACME.5, 2, mean)

s.1 <- apply(ACME.1, 2, sd)
s.2 <- apply(ACME.2, 2, sd)
s.3 <- apply(ACME.3, 2, sd)
s.4 <- apply(ACME.4, 2, sd)
s.5 <- apply(ACME.5, 2, sd)

coefs <- cbind(c.1, c.2, c.3, c.4, c.5)
c.combined <- apply(coefs, 1, mean)

var.within <- cbind(s.1^2, s.2^2, s.3^2, s.4^2, s.5^2)
var.within <- apply(var.within, 1, mean)

m <- 5
var.between <- (coefs - c.combined)^2
var.between <-  apply(var.between, 1, sum)
var.between <-  (m-1)^-1 * var.between

s.combined <- sqrt(var.within + (1 + m^-1) * var.between )

combined.res <- cbind(c.combined, s.combined)
combined.res <- round(combined.res, 2)

av.ACME.mi <- combined.res[3,1]
av.ACME.se.mi <- combined.res[3,2]
